package com.zretc.support.tool;

import com.zretc.support.entity.Course;
import com.zretc.support.entity.Topicmult;
import com.zretc.support.entity.Topicsingle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 通过excel导入试卷  -- 将试题存储在map中
 */
public class ImportExamPaper {

    // 参数记得要传
    public Map<Integer,Object> importExamPaper(long courseId, MultipartFile file) throws Exception {
        // 多态 抛异常
        Workbook sheets = new XSSFWorkbook(file.getInputStream());
        //获取一个工作表(sheet页)，下标从0开始
        Sheet sheet = sheets.getSheetAt(0);

        List<Topicmult> list = new ArrayList<>();

        List<Topicsingle> list1 = new ArrayList<>();

        for (int i = 1; i<=sheet.getLastRowNum() ; i++) {

            // 获取行数
            Row row = sheet.getRow(i);
            row.getCell(0).setCellType(CellType.STRING);
            row.getCell(5).setCellType(CellType.STRING);
            row.getCell(6).setCellType(CellType.STRING);
            row.getCell(7).setCellType(CellType.STRING);
            row.getCell(8).setCellType(CellType.STRING);
            row.getCell(9).setCellType(CellType.STRING);
            String value2 = null;
            String value3 = null;
            String value4 = null;
            String value5 = null;

            if(row.getCell(1) != null){
                row.getCell(1).setCellType(CellType.STRING);
                value2 = row.getCell(1).getStringCellValue();
            }
            if(row.getCell(2) != null){
                row.getCell(2).setCellType(CellType.STRING);
                value3 = row.getCell(2).getStringCellValue();
            }
            if(row.getCell(3) != null){
                row.getCell(3).setCellType(CellType.STRING);
                value4 = row.getCell(3).getStringCellValue();
            }
            if(row.getCell(4) != null){
                row.getCell(4).setCellType(CellType.STRING);
                value5 = row.getCell(4).getStringCellValue();
            }

            // 获取单元格 取值
            String value1 = row.getCell(0).getStringCellValue();
            String value6 = row.getCell(5).getStringCellValue();
            String value7 = row.getCell(6).getStringCellValue();
            String value8 = row.getCell(7).getStringCellValue();
            String value9 = row.getCell(8).getStringCellValue();
            String value10 = row.getCell(9).getStringCellValue();

            Course course = new Course();
            course.setCourseId(courseId);
            // 如果 2 | 3 | 4 | 5 为空 -- 则是试题表
            if(value2 == null){

                // 添加到试题表中
                Topicsingle topicsingle = new Topicsingle(value1,value6,
                        Double.parseDouble(value7),Integer.parseInt(value8),
                        Integer.parseInt(value9),value10,course);
                list1.add(topicsingle);
            }else{
                // 否则是选择题表
                Topicmult topicmult = new Topicmult(value1,value2,value3,value4,
                        value5,value6,Double.parseDouble(value7),Integer.parseInt(value8),
                        Integer.parseInt(value9),value10,course);
                list.add(topicmult);
            }
        }

        Map<Integer,Object> map = new HashMap<>();
        // 1.选择
        map.put(1,list);
        // 2.试题
        map.put(2,list1);

        //关流
        sheets.close();

        return map;
    }

}
